package com.ruoyi.project.mobile.home;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.aboutus.domain.AboutUs;
import com.ruoyi.project.system.aboutus.service.IAboutUsService;
import com.ruoyi.project.system.banner.service.IBannerService;
import com.ruoyi.project.system.goods.goods.domain.GoodsDto;
import com.ruoyi.project.system.goods.goods.domain.GoodsGoods;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goodsclass.domain.GoodsClass;
import com.ruoyi.project.system.goodsclass.enums.ClassIsHome;
import com.ruoyi.project.system.goodsclass.enums.ClassStatus;
import com.ruoyi.project.system.goodsclass.service.IGoodsClassService;
import com.ruoyi.project.system.hotword.domain.HotWord;
import com.ruoyi.project.system.hotword.service.IHotWordService;
import com.ruoyi.project.system.material.center.domain.MaterialCenter;
import com.ruoyi.project.system.material.center.service.IMaterialCenterService;
import com.ruoyi.project.system.material.img.domain.MaterialImg;
import com.ruoyi.project.system.material.img.service.IMaterialImgService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.messagenotice.domain.MessageNotice;
import com.ruoyi.project.system.messagenotice.service.IMessageNoticeService;
import com.ruoyi.project.system.newregister.service.INewRegisterService;
import com.ruoyi.project.system.proclamation.domain.Proclamation;
import com.ruoyi.project.system.proclamation.enums.ProclamationStatus;
import com.ruoyi.project.system.proclamation.service.IProclamationService;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/18 18:19
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/home")
public class HomeController extends BaseController {

	@Autowired
	private IBannerService bannerService;
	@Autowired
	private IGoodsClassService goodsClassService;
	@Autowired
	private IProclamationService proclamationService;
	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private IHotWordService hotWordService;
	@Autowired
	private IShopConfigService shopConfigService;
	@Autowired
	private IMemberService memberService;
	@Autowired
    private INewRegisterService newRegisterService;
	@Autowired
    private IMaterialCenterService materialCenterService;
	@Autowired
    private IMaterialImgService materialImgService;
	@Autowired
    private IAboutUsService aboutUsService;
	@Autowired
    private IMessageNoticeService messageNoticeService;

	/**
	 * 查询轮播图
	 * @param uid
	 * @return
	 */
	@GetMapping("/banners")
	public R homeBanners(@RequestParam String uid,@RequestParam Integer address){

		return RFactory.generateR(this.bannerService.findBannerList(address));
	}

	/**
	 * 查询分类
	 * @param uid
	 * @return
	 */
	@GetMapping("/class")
	public R homeClass(@RequestParam String uid){
		GoodsClass goodsClass = new GoodsClass();
		goodsClass.setIsHome(ClassIsHome.NORMAL.getValue());
		goodsClass.setStatus(ClassStatus.NORMAL.getValue());
		return RFactory.generateR(this.goodsClassService.selectGoodsClassList(goodsClass));
	}

	/**
	 * 查询公告
	 * @param uid
	 * @return
	 */
	@GetMapping("/proclamations")
	public R homeProclamations(@RequestParam String uid){
		Proclamation proclamation = new Proclamation();
		proclamation.setStatus(ProclamationStatus.SHOW.getValue());
		return RFactory.generateR(this.proclamationService.selectProclamationList(proclamation));
	}


	/**
	 *
	 * @param brand 是否是品牌大牌 0 不是 1 是
	 * @param abroad 是否是境外产品 0 不是 1 是
	 * @param recommend 是否是推荐产品 0 不是 1 是
	 * @param hot 是否是热卖产品 0 不是 1 是
	 * @param spike 是否是秒杀产品 0 不是 1 是
	 * @return
	 */
	@GetMapping("/goods")
	@Transactional
	public R homeGoods(@RequestParam String uid,
						@RequestParam(required = false,defaultValue = "") Integer brand,
						@RequestParam(required = false,defaultValue = "") Integer abroad,
						@RequestParam(required = false,defaultValue = "") Integer recommend,
						@RequestParam(required = false,defaultValue = "") Integer hot,
						@RequestParam(required = false,defaultValue = "") Integer spike,
						@RequestParam(required = false,defaultValue = "") Integer status,
						@RequestParam(required = false,defaultValue = "") List statusList,
						@RequestParam(required = false,defaultValue = "") String name){
		startPage();
		GoodsGoods goodsGoods = new GoodsGoods();
		goodsGoods.setIsAbroad(abroad);
		goodsGoods.setIsBrand(brand);
		goodsGoods.setIsHot(hot);
		goodsGoods.setIsRecommend(recommend);
		goodsGoods.setHasSpike(spike);
		goodsGoods.setStatus(status);
		goodsGoods.setName(name);
		goodsGoods.setStatusList(statusList);
		//根据会员等级查询价格
		Member member = this.memberService.selectMemberById(uid);
		if(member!=null){
			goodsGoods.setVip(member.getIsVip());
		}else{
			goodsGoods.setVip(0);
		}
        List<GoodsDto> goodsDtoList = this.goodsGoodsService.findGoodsDtoList(goodsGoods);
		//添加每日爆品时间
//        int dayHotTime = this.shopConfigService.findAll().get(0).getDayHotTime();//每日爆品时间 秒数
        int sec = this.shopConfigService.findAll().get(0).getDayHotTime();//每日爆品时间 秒
        long nowTime = DateUtils.getNowDate().getTime();//现在时间 时间戳
        if (goodsDtoList != null) {
            for(GoodsDto GoodsDtos:goodsDtoList){
//                if (GoodsDtos.getCreated() == null) {
//                    long endTime=nowTime+(sec * 1000L);//结束时间 时间戳
//                    long deadtime=endTime-nowTime;
//                    long dayHotTime=deadtime/1000L;
//                    GoodsDtos.setDayHotTime(dayHotTime);
//                }
                long createdTime = GoodsDtos.getCreated().getTime();//创建时间 时间戳
                long endTime=createdTime+(sec * 1000L);//结束时间 时间戳
                long deadtime=endTime-nowTime;
                long dayHotTime=deadtime/1000L;
                GoodsDtos.setDayHotTime(dayHotTime);
            }
        }
        if(!StringUtils.isBlank(name)){
			//判断是否搜索过
			HotWord oldHotWord= this.hotWordService.findByName(name,uid);
			if(oldHotWord==null){
				//新增搜索历史
				HotWord hotWord = new HotWord();
				hotWord.setName(name);
				hotWord.setCreated(DateUtils.getNowDate());
				hotWord.setType(1);
				hotWord.setMemberId(uid);
				this.hotWordService.insertHotWord(hotWord);
			}else{
				//更新搜索时间
				HotWord update = new HotWord();
				update.setId(oldHotWord.getId());
				update.setCreated(DateUtils.getNowDate());
				this.hotWordService.updateHotWord(update);
			}
		}
		return RFactory.generateR(getDataTable(goodsDtoList));
	}

	/**
	 * 查询热词
	 * @param uid
	 * @return
	 */
	@GetMapping("/hotwords")
	public R homeHotWord(@RequestParam String uid){
		return RFactory.generateR(this.hotWordService.findAll(uid));
	}


	/**
	 * 清空用户历史搜索
	 * @param uid
	 * @return
	 */
	@DeleteMapping("/hotwords")
	@Transactional
	public R deleteSearch(@RequestParam String uid){
		this.hotWordService.deleteByUid(uid);
		return R.ok();
	}

	/**
	 * 清除某条历史搜索
	 * @param id
	 * @return
	 */
	@DeleteMapping("/hotwords/one")
	@Transactional
	public R deleteOneSearch(@RequestParam String id){
		this.hotWordService.deleteHotWordById(id);
		return R.ok();
	}

	/**
	 * 查询首页配置信息
	 * @param uid
	 * @return
	 */
	@GetMapping("/config")
	public R homeConfig(@RequestParam String uid){
		return RFactory.generateR(this.shopConfigService.findAll().get(0));
	}

	/**
	 * 查询是否是新人
	 */
	@GetMapping("/isnew")
	public R memberIsNew(@RequestParam String uid){
		return RFactory.generateR(this.memberService.selectMemberById(uid).getIsNew());
	}
	/**
	 * 查询是否是代理
	 */
	@GetMapping("/isvip")
	public R memberIsvip(@RequestParam String uid){
		return RFactory.generateR(this.memberService.selectMemberById(uid).getIsVip());
	}

    /**
     * 查询新人注册配置信息
     * @param uid
     * @return
     */
    @GetMapping("/newRegister")
    public R newRegister(@RequestParam String uid){
        return RFactory.generateR(this.newRegisterService.findAll().get(0));
    }

    /**
     * 查询素材中心信息
     * @param uid
     * @return
     */
    @GetMapping("/MaterialCenter")
    public R MaterialCenter(@RequestParam String uid){
        startPage();
//        MaterialCenter materialCenter = new MaterialCenter();
//        materialCenter.setIsShow(1);
        List<MaterialCenter> list = this.materialCenterService.findMaterialList();
        if(list!=null){
            for(MaterialCenter materialCenter1:list){
                MaterialImg img = new MaterialImg();
                img.setMaterialId(materialCenter1.getId());
                materialCenter1.setMaterialImgs(this.materialImgService.selectMaterialImgList(img));
            }
        }
        return RFactory.generateR(getDataTable(list));
    }

    /**
     * 根据id查询素材中心图片
     * @param id
     * @return
     */
    @GetMapping("/MaterialImg")
    public R MaterialImg(@RequestParam String id){
        return RFactory.generateR(this.materialImgService.findListByMaterialIdAndType(id,0));
    }

    /**
     * 查询关于我们列表
     * @param uid
     * @return
     */
    @GetMapping("/aboutUsList")
    public R aboutUsList(@RequestParam String uid) {
        startPage();
        AboutUs aboutUs = new AboutUs();
        aboutUs.setIsShow(1);
        return RFactory.generateR(getDataTable(this.aboutUsService.selectAboutUsList(aboutUs)));
    }

    /**
     * 根据ID查询关于我们
     * @param id 关于我们ID
     * @return
     */
    @GetMapping("/detailsById")
    public R detailsById(@RequestParam String id) {
        return RFactory.generateR(this.aboutUsService.selectAboutUsById(id));
    }

    /**
     * 查询消息通知公告列表
     * @param uid
     * @return
     */
    @GetMapping("/messageNoticeList")
    public R messageNoticeList(@RequestParam String uid,Integer type) {
        startPage();
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setIsShow(1);
        messageNotice.setType(type);
        return RFactory.generateR(getDataTable(this.messageNoticeService.selectMessageNoticeList(messageNotice)));
    }

    /**
     * 根据ID消息通知公告详情
     * @param id 关于我们ID
     * @return
     */
    @GetMapping("/messageNoticeById")
    public R messageNoticeById(@RequestParam String id) {
        return RFactory.generateR(this.messageNoticeService.selectMessageNoticeById(id));
    }

    /**
     * 查询开团申请商品列表
     * @param uid
     * @return
     */
    @GetMapping("/shoppingApplyList")
    public R shoppingApplyList(@RequestParam(required = false,defaultValue = "") String name, String uid) {
        startPage();
        GoodsGoods goodsGoods = new GoodsGoods();
        goodsGoods.setName(name);
        goodsGoods.setStatus(5);
        return RFactory.generateR(getDataTable(this.goodsGoodsService.selectGoodsGoodsList(goodsGoods)));
    }

    /**
     * 商品点击开团
     * @param goodsId 商品id
     * @return
     */
    @GetMapping("/clickShoppingApply")
    public R clickShoppingApply(@RequestParam String goodsId) {
        //通过商品ID查询
        GoodsGoods goodsById = goodsGoodsService.selectGoodsGoodsById(goodsId);
        if (goodsById.getApplyNum() != null) {
            goodsById.setApplyNum(goodsById.getApplyNum()+1);
            goodsGoodsService.updateGoodsGoods(goodsById);
        } else {
            goodsById.setApplyNum(1);
            goodsGoodsService.updateGoodsGoods(goodsById);
        }

        return R.ok();
    }

    /**
     * 开团申请搜索
     * @param name 商品名
     * @return
     */
    @GetMapping("/shoppingApplySearch")
    public R shoppingApplySearch(@RequestParam(required = false,defaultValue = "") String name) {
        startPage();
        GoodsGoods goodsGoods = new GoodsGoods();
        goodsGoods.setName(name);
        goodsGoods.setStatus(5);
        return RFactory.generateR(getDataTable(this.goodsGoodsService.selectGoodsGoodsList(goodsGoods)));
    }

}
